<?php

class Admin_TemplateController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $this->_helper->layout->setLayout('admin');	
       Zend_Loader::loadFile('../application/admin/models/Template.php');
       $template_model = new Template();
       $template_arr = $template_model->listTemplates();
       // print_r($newsletter_arr); EXIT;
       $this->view->template_arr = $template_arr;
     
    }

    public function addAction()
    {
        $this->_helper->layout->setLayout('admin');	
		Zend_Loader::loadFile('../application/admin/forms/Template.php');
        $template_form = new Form_Template();
        $this->view->title = "Add new Template";
        
        $this->view->template_form = $template_form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($template_form->isValid($formData)) {
                $template_title = $template_form->getValue('template_title');
                $template_content = $template_form->getValue('template_content');
                $module_name= $template_form->getValue('module_name');
                $is_default=$template_form->getValue('is_default');
                $template_id = $template_form->getValue('template_id');
              
           Zend_Loader::loadFile('../application/admin/models/Template.php');
           $template_model = new Template();
           $template_model->addTemplate($template_title, $template_content,$module_name,$is_default);
           $this->_helper->redirector('index');
            } else {
                $template_form->populate($formData);
            }
        }
            
    }

    public function editAction()
    {
    	$this->_helper->layout->setLayout('admin');	
        Zend_Loader::loadFile('../application/admin/forms/Template.php');
        $template_form = new Form_Template();
        $this->view->form = $template_form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($template_form->isValid($formData)) {
                $template_id = (int)$template_form->getValue('template_id');
                $template_title = $template_form->getValue('template_title');
                $template_content = $template_form->getValue('template_content');
                $module_name= $template_form->getValue('module_name');
                $is_default=$template_form->getValue('is_default');
                $template_id = $template_form->getValue('template_id');
                Zend_Loader::loadFile('../application/admin/models/Template.php');
                $template_model = new Template();
                $template_model->updateTemplate($template_id, $template_title, $template_content,$module_name,$is_default);
                
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $template_id = $this->_getParam('template_id', 0);
            if ($template_id > 0) {
                Zend_Loader::loadFile('../application/admin/models/Template.php');
                $template_model = new Template();
                $template_form->populate($template_model->getTemplate($template_id));
            }
        }
        
    }

   /* public function deleteAction()
    {
      $this->_helper->layout->setLayout('admin');	
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
               $template_id = $this->getRequest()->getPost('template_id');
                Zend_Loader::loadFile('../application/admin/models/Template.php');
                $template_model = new template();
                $template_model->deleteTemplate($template_id);
            }
            $this->_helper->redirector('index');
        } else {
            $template_id = $this->_getParam('template_id', 0);
            Zend_Loader::loadFile('../application/admin/models/Template.php');
            $template_model = new template();
            $this->view->template = $template_model->getTemplate($template_id);
        }
    }*/

        public function deleteAction()
          {
               $this->_helper->layout->setLayout('admin');	
               $template_id = $this->_getParam('template_id', 0);
           //    echo $hotel_id;exit;
                Zend_Loader::loadFile('../application/admin/models/Template.php');
                $Template_model = new Template();
                $Template_model->deleteTemplate($template_id);
                 $this->_helper->redirector('index');
      
       
           }
           
      public function gettemplateAction()
          {

                $template_id=$_POST['template_id'];
                Zend_Loader::loadFile('../application/admin/models/Template.php');
                $Template_model = new Template();
                $template= $Template_model->getTemplatenewsletter($template_id);
               
                $html=$template['template_content'];          	
                
		         
		      echo $html ;exit;
		       
      
       
           }
    
}







